package com.ccsc.auth.service.impl;

import com.ccsc.auth.dao.RoleDao;
import com.ccsc.auth.dao.UserDao;
import com.ccsc.auth.entity.Resource;
import com.ccsc.auth.entity.Role;
import com.ccsc.auth.entity.Systems;
import com.ccsc.auth.service.RoleService;
import com.ccsc.auth.utils.DataMap;
import com.ccsc.auth.utils.Page;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.PrivateKey;
import java.util.List;

/**
 * Created by yyangs on 2017/2/15.
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserDao userDao;

    @Override
    public List<Role> getAllRoles() throws Exception {
        return roleDao.getAllRoles();
    }

    @Override
    public List<Role> getRolesByStatus(Integer status) throws Exception {
        Role role=new Role();
        role.setStatus(status);
        return roleDao.getAllRolesByCondition(role);
    }

    @Override
    public List<Role> getRolesByName(String name) throws Exception {
        Role role=new Role();
        role.setName(name);
        return roleDao.getAllRolesByCondition(role);
    }

    @Override
    public List<Role> getRolesByResourceId(Integer resourceId) throws Exception {
        return roleDao.getAllRolesByResourceId(resourceId);
    }

    @Override
    public List<Role> getRolesByUserId(Integer userId) throws Exception {
        return roleDao.getAllRolesByUserId(userId);
    }

    @Override
    public Page getRolesByPage(DataMap params) throws Exception {
        List<Role> resultList = roleDao.getAllRoles(new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Role> pageList = (PageList<Role>) resultList;
        return new Page(pageList);
    }

    @Override
    public Page getRolesByPage(Role role, DataMap params) throws Exception {
        List<Role> resultList = roleDao.getAllRolesByCondition(role,new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Role> pageList = (PageList<Role>) resultList;
        return new Page(pageList);
    }

    @Override
    public Role getRoleById(Integer id) throws Exception {
        Role role=new Role();
        role.setId(id);
        return roleDao.getRoleById(role);
    }

    @Override
    public Integer createRole(Role role) throws Exception {
        roleDao.createRole(role);
        //
        return roleDao.createRoleAndResources(role);
    }

    @Override
    public Integer updateRole(Role role) throws Exception {
        if(role.getResourceList()!=null){
            roleDao.deleteRoleAndResources(role);
            roleDao.createRoleAndResources(role);
        }
        return roleDao.updateRole(role);
    }

    @Override
    public Integer deleteRoleById(Integer id) throws Exception {
        Role role=new Role();
        role.setId(id);
        roleDao.deleteRoleAndResources(role);
        DataMap dataMap=new DataMap();
        dataMap.put("user", null);
        dataMap.put("roleId",id);
        userDao.deleteUserAndRole(dataMap);
        return roleDao.deleteRole(role);
    }

    @Override
    public Integer activeRole(Integer id) throws Exception {
        Role role=new Role();
        role.setId(id);
        role.setStatus(1);
        return roleDao.updateRole(role);
    }

    @Override
    public Integer withdrawRole(Integer id) throws Exception {
        Role role=new Role();
        role.setId(id);
        role.setStatus(0);
        return roleDao.updateRole(role);
    }

    @Override
    public Integer batchActiveRole(List<Integer> ids) throws Exception {
        int result=0;
        for (Integer id:ids) {
            result=activeRole(id);
        }
        return result;
    }

    @Override
    public Integer batchWithdrawRole(List<Integer> ids) throws Exception {
        int result=0;
        for (Integer id:ids) {
            result=withdrawRole(id);
        }
        return result;
    }

    @Override
    public Integer getUserNumByRoleId(Integer roleId) throws Exception {
        return roleDao.getUserNumByRoleId(roleId);
    }
}
